package com.da.javatest.niuke.baodian;

/**
 * @author chenlida
 * @date 2020/10/7 15:35
 * https://www.nowcoder.com/practice/743853af75fc4026939a682b86535f79?tpId=8&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C27BinDecimal {
    //十进制的小数转换为二进制，主要是小数部分乘以2，
    //取整数部分依次从左往右放在小数点后，直至小数点后为0。
    public static String printBin(double num) {
        StringBuilder sb = new StringBuilder("0.");
        int count = 0;
        while (num > 0) {
            count++;
            if (count > 32) { return "Error"; }
            num = num * 2;
            if (num >= 1) {
                sb.append("1");
                num = num - 1;
            } else {
                sb.append("0");
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(printBin(0.625d));
        System.out.println(printBin(0.825d));
    }
}
